package com.ruoyi.slope.data.geoacoustic.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.slope.data.geoacoustic.domain.SlopeGeoacousticData;
import com.ruoyi.slope.data.geoacoustic.domain.SlopeGeoacousticStatistic;
import com.ruoyi.slope.data.geoacoustic.service.ISlopeGeoacousticDataService;
import com.ruoyi.slope.dataFile.domain.SlopeDataFile;
import com.ruoyi.slope.dataFile.service.ISlopeDataFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 地声传感器监测数据Controller
 * 
 * @author psy
 * @date 2025-05-02
 */
@RestController
@RequestMapping("/data/geoacoustic")
public class SlopeGeoacousticDataController extends BaseController
{    
    @Autowired
    private ISlopeDataFileService slopeDataFileService;
    @Autowired
    private ISlopeGeoacousticDataService slopeGeoacousticDataService;

    /**
     * 查询地声传感器监测数据列表
     */
    @PreAuthorize("@ss.hasPermi('deviceInfo:deviceInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(SlopeGeoacousticData slopeGeoacousticData)
    {
        startPage();
        List<SlopeGeoacousticData> list = slopeGeoacousticDataService.selectSlopeGeoacousticDataList(slopeGeoacousticData);
        return getDataTable(list);
    }

    /**
     * 导出地声传感器监测数据列表
     */
    @PreAuthorize("@ss.hasPermi('deviceInfo:deviceInfo:list')")
    @Log(title = "地声传感器监测数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SlopeGeoacousticData slopeGeoacousticData)
    {
        List<SlopeGeoacousticData> list = slopeGeoacousticDataService.selectSlopeGeoacousticDataList(slopeGeoacousticData);
        ExcelUtil<SlopeGeoacousticData> util = new ExcelUtil<SlopeGeoacousticData>(SlopeGeoacousticData.class);
        String path = util.exportExcel(response, list, "地声传感器监测数据数据");
        SlopeDataFile slopeDataFile = new SlopeDataFile();
        slopeDataFile.setFileName(path);
        slopeDataFileService.insertSlopeDataFile(slopeDataFile);
    }

    /**
     * 导出次声数据统计列表
     */
    @PreAuthorize("@ss.hasPermi('deviceInfo:deviceInfo:list')")
    @Log(title = "边坡次声监测数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export/geoacoustic")
    public void exportCrackXAcc(HttpServletResponse response, SlopeGeoacousticData slopeGeoacousticData) {
        // 获取数据列表
        List<SlopeGeoacousticData> list = slopeGeoacousticDataService.selectSlopeGeoacousticDataList(slopeGeoacousticData);

        // 计算统计数据
        double OSPAverage = list.stream().mapToDouble(SlopeGeoacousticData::getOriginalSoundPressure).average().orElse(0);
        double OSPMax = list.stream().mapToDouble(SlopeGeoacousticData::getOriginalSoundPressure).max().orElse(0);
        double OSPRms = Math.sqrt(list.stream().mapToDouble(data -> Math.pow(data.getOriginalSoundPressure(), 2)).average().orElse(0));

        // 计算统计数据
        double VSPAverage = list.stream().mapToDouble(SlopeGeoacousticData::getEffectiveSoundPressure).average().orElse(0);
        double VSPMax = list.stream().mapToDouble(SlopeGeoacousticData::getEffectiveSoundPressure).max().orElse(0);
        double VSPRms = Math.sqrt(list.stream().mapToDouble(data -> Math.pow(data.getEffectiveSoundPressure(), 2)).average().orElse(0));

        // 计算统计数据
        double freqAverage = list.stream().mapToDouble(SlopeGeoacousticData::getFrequencySignal).average().orElse(0);
        double freqMax = list.stream().mapToDouble(SlopeGeoacousticData::getFrequencySignal).max().orElse(0);
        double freqRms = Math.sqrt(list.stream().mapToDouble(data -> Math.pow(data.getFrequencySignal(), 2)).average().orElse(0));

        // 创建一个新的列表，把统计数据加到列表顶部
        List<SlopeGeoacousticStatistic> dataWithStatistics = new ArrayList<>();

        // 可以创建一个虚拟的数据对象用来承载统计数据
        SlopeGeoacousticStatistic statisticsData = new SlopeGeoacousticStatistic();
        statisticsData.setAverageOSP(OSPAverage);
        statisticsData.setAbsoluteMaxOSP(OSPMax);
        statisticsData.setRootMeanSquareOSP(OSPRms);
        statisticsData.setAverageESP(VSPAverage);
        statisticsData.setAbsoluteMaxESP(VSPMax);
        statisticsData.setRootMeanSquareESP(VSPRms);
        statisticsData.setAverageFreq(freqAverage);
        statisticsData.setAbsoluteMaxFreq(freqMax);
        statisticsData.setRootMeanSquareFreq(freqRms);
        dataWithStatistics.add(statisticsData);  // 添加统计数据到列表


        for(SlopeGeoacousticData data : list){
            SlopeGeoacousticStatistic statistics = new SlopeGeoacousticStatistic();
            statistics.setOriginalSoundPressure(data.getOriginalSoundPressure());
            statistics.setEffectiveSoundPressure(data.getEffectiveSoundPressure());
            statistics.setFrequencySignal(data.getFrequencySignal());
            statistics.setUpdateTime(data.getUpdateTime());
            statistics.setDeviceSn(data.getDeviceSn());
            dataWithStatistics.add(statistics);
        }

        // 生成 Excel
        ExcelUtil<SlopeGeoacousticStatistic> util = new ExcelUtil<>(SlopeGeoacousticStatistic.class);

        // 导出带统计数据的 Excel 文件
        String path = util.exportExcel(response, dataWithStatistics, "边坡裂缝传感器监测数据数据"); 
        SlopeDataFile slopeDataFile = new SlopeDataFile();
        slopeDataFile.setFileName(path);
        slopeDataFileService.insertSlopeDataFile(slopeDataFile);
    }

}
